<template>
  <div>
    <!-- 选择客户 -->
    <el-dialog title="信息" :visible.sync="openSelectCustomer" width="1200px" append-to-body :destroy-on-close="true" :before-close="beforeClose">
      <el-form ref="form" @submit.native.prevent :model="formQuery" label-width="100px" :inline="true" v-show="showSearch">
        <el-form-item label="客户" prop="khmc">
          <el-select
            v-model="formQuery.khmc"
            placeholder="请选择客户"
            clearable
            filterable
            @change="selectCustomerChange"
            @keyup.enter.native="enterClick"
            :loading = "selectClientLoading"
          >
            <el-option
                v-for="(item, index) in selectClientList"
                :key="index"
                :label="item.khmc"
                :value="item.khmc"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="仓库" prop="spckmc">
          <el-select
            v-model="formQuery.spckmc"
            placeholder="请选择"
            clearable
            filterable
            @keyup.enter.native="enterClick"
            :loading="selectWarehouse3Loading"
            @change="changeSelectWarehouse3"
          >
            <el-option
                v-for="item in selectWarehouse3List"
                :key="item.spckid"
                :label="item.spckmc"
                :value="item.spckmc"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="柜号/批号" prop="spgh">
          <el-input
            v-model="formQuery.spgh"
            placeholder="请输入柜号/批号"
            clearable
            @keyup.enter.native="enterClick"
          />
        </el-form-item>
        <el-form-item>
            <el-button type="primary" size="mini" @click="handleQuery">搜 索</el-button>
        </el-form-item>
      </el-form>
      <el-table
        v-loading="loading"
        :data="customerList"
      >
        <af-table-column label="操作" align="center" class-name="small-padding fixed-width" >
            <template slot-scope="scope">
            <el-button
                size="mini"
                type="text"
                @click="handleSelect(scope.row)"
            >录入</el-button>
            </template>
        </af-table-column>
        <af-table-column prop="khmc" label="客户" width="250"></af-table-column>
        <af-table-column prop="spckmc" label="仓库" ></af-table-column>
        <af-table-column prop="spjs" label="剩余件数" ></af-table-column>
        <af-table-column prop="spsl" label="剩余重量(吨)" ></af-table-column>
      </el-table>
       <pagination
        v-show="total>0"
        :total="total"
        :page.sync="pageNum"
        :limit.sync="pageSize"
        @pagination="getList"
      />
    </el-dialog>
  </div>

</template>

<script>
import { selectCustomer, selectClient } from "@/api/storage/commonGoods/warehouseMove";
import { selectWarehouse3 } from "@/api/storage/warehouse"

export default {
  name: "WarehouseMoveSelectCustomer",
  data() {
    return {
      // 显示搜索条件
      showSearch: true,
      //弹窗状态
      openSelectCustomer: false,
      //获取客户加载
      selectClientLoading: true,
      //仓库加载
      selectWarehouse3Loading: true,
      //列表加载
      loading: true,
      // 查询参数
      formQuery: {
        isBa: "",
        khmc: "",
        khpb: 0,
        shopId: 0,
        spckidis: [],
        spckmc: "",
	      spgh: "" 
      },
      //行数据
      customerList: [],
      //客户列表
      selectClientList: [],
       //库存列表
       selectWarehouse3List: [],
      //页数
      pageSize: 10,
      //页码
      pageNum:1,
      // 总条数
      total: 0,
    };
  },
  methods: {
     /**获取仓库 */
     stockSelect() {
      this.selectWarehouse3Loading = true
      selectWarehouse3({}).then(response => {
        this.selectWarehouse3List = response.data;
        this.selectWarehouse3Loading = false
      });
    },
    /**获取仓库信息 */
    getList() {
      this.loading = true
      selectCustomer(this.formQuery, {pageNum:this.pageNum, pageSize:this.pageSize}).then(response => {
        this.customerList = response.rows;
        this.total = response.total
        this.loading = false
      });
    },
    /**获取客户 */
    clientSelect() {
        this.selectClientLoading = true
       selectClient({}).then(response => {
        this.selectClientList = response.data
        this.selectClientLoading = false
      });
    },
    changeSelectWarehouse3() {
      this.getList()
    },
    /**仓库搜索 */
    handleQuery() {
        this.getList()
    },
    /** 选择库存*/
    handleSelect(row) {
      this.$emit("customerSelected", row)
      this.close()
    },
    /**客户改变 */
    selectCustomerChange() {
      this.getList()
    },
      /**enter键触发 */
    enterClick(e) {
      this.getList()
    },
    /**改变窗口状态 */
    changeSelectCustomer() {
      this.openSelectCustomer = !this.openSelectCustomer;
    },
    /**关闭弹窗 */
    beforeClose(done) {
      this.close()
      this.reset()
      done()
    },
    reset() {
      this.formQuery = {
        isBa: "",
        khmc: "",
        khpb: 0,
        shopId: 0,
        spckidis: [],
        spckmc: "",
	      spgh: "" 
      }
      this.customerList = []
      this.selectClientList = []
    },
    /**关闭弹窗 */
    close() {
      this.changeSelectCustomer()
    },
    /**打开弹窗 */
    open() {
        this.changeSelectCustomer()
        this.getList()
        this.stockSelect()
        this.clientSelect()
    }
  }
};
</script>
